package ils.web.action;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import ils.domain.model.User;
import ils.services.AdminService;
import ils.util.SpringUtil;
import ils.util.Util;
import org.apache.struts2.interceptor.SessionAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.Set;

public class LoginAction extends ActionSupport implements SessionAware, Preparable
{
    private static final Logger logger = LoggerFactory.getLogger(LoginAction.class);
    private String email;
    private String              password;
    private String              target;
    private Map<String, Object> session;
    private AdminService adminService;
    public String execute() throws Exception
    {
        if (email == null || password == null)
        {
            logger.debug("either username or password is null");
            return Action.INPUT;
        }
        else
        {
            User user = null;
            try {
                user = adminService.login(email, Util.md5(password));
            } catch (Exception e) {
                logger.error("failed login attempt", e);
                return INPUT;
            }
            if(user == null)
            {
                logger.debug("email and password do not match");
                return INPUT;
            }else
            {
                logger.debug("successfull login for " + user.getFirstName());
                session.put("userId", user.getId());
                session.put("userName", user.getFirstName());
                session.put("login", "true");
                session.put("role", user.getRoles());
            }
            if(target != null)
            {
                logger.debug("target: " + target);
                return "redirect";
            }
            /*else if(user.getUserType().equals(User.UserType.librarian.name()))
            {
                return User.UserType.librarian.name();
            }*/
            else 
            {
                Set<String> roles = user.getRoles();
                if(roles.contains("admin")) return "admin";
                else if(roles.contains("librarian")) return "librarian";
                else if(roles.contains("user")) return "user";
                else throw new RuntimeException("not roles found");
            }
        }
    }   

    public void setPassword(String password)
    {
        this.password = password;
    }

    public String getTarget()
    {
        return target;
    }

    public void setTarget(String target)
    {
        this.target = target;
    }

    @Override
    public void setSession(Map<String, Object> arg0)
    {
       this.session = arg0; 
    }

    public void setAdminService(AdminService adminService)
    {
        this.adminService = adminService;
    }

    public String getEmail()
    {
        return email;
    }

    public void setEmail(String email)
    {
        this.email = email;
    }

    @Override
    public void prepare() throws Exception {
        adminService = (AdminService)SpringUtil.getApplicationContext().getBean("adminService");
    }
}
